package com.zklh.leetcode.interviewTop150;

/**
 * 删除有序数组中的重复项
 *
 * @Author tongnan
 * @Date 2024/4/25
 * @Version 1.0
 */
public class Solution80 {
    public int removeDuplicates(int[] nums) {
        int current = 0;
        int prevNum = nums[0];
        int repeatTwice = 0;
        int latest = 0;
        while (current < nums.length) {
            if (nums[current] == prevNum) {
                if (++repeatTwice > 2) {
                    current++;
                } else {
                    nums[latest++] = nums[current++];
                }
            } else {
                prevNum = nums[current];
                repeatTwice = 1;
                nums[latest++] = nums[current++];
            }
        }
        return latest;
    }
}